<template>
  <div class="register-container">
    <div class="register-wrapper">
      <div class="register-header">
        <el-icon class="logo-icon"><HomeFilled /></el-icon>
        <h1 class="system-title">脑卒中溶栓治疗平台</h1>
        <p class="subtitle">创建新账号</p>
      </div>
      
      <el-form :model="registerForm" :rules="rules" ref="registerFormRef" class="register-form">
        <el-form-item prop="username">
          <el-input
            v-model="registerForm.username"
            placeholder="请输入用户名"
            prefix-icon="User"
            :validate-event="false"
          ></el-input>
        </el-form-item>
        
        <el-form-item prop="email">
          <el-input
            v-model="registerForm.email"
            placeholder="请输入邮箱"
            prefix-icon="Message"
            :validate-event="false"
          ></el-input>
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="registerForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="Lock"
            :validate-event="false"
            show-password
          ></el-input>
        </el-form-item>
        
        <el-form-item prop="confirmPassword">
          <el-input
            v-model="registerForm.confirmPassword"
            type="password"
            placeholder="请确认密码"
            prefix-icon="Lock"
            :validate-event="false"
            show-password
          ></el-input>
        </el-form-item>
        
        <el-form-item prop="agreement" class="agreement-checkbox">
          <el-checkbox v-model="registerForm.agreement">
            我已阅读并同意
            <el-link type="primary" :underline="false">《用户协议》</el-link>
            和
            <el-link type="primary" :underline="false">《隐私政策》</el-link>
          </el-checkbox>
        </el-form-item>
        
        <el-form-item>
          <el-button 
            type="primary" 
            class="register-button" 
            :loading="loading" 
            @click="handleRegister"
          >
            注册
          </el-button>
        </el-form-item>
        
        <div class="login-link">
          <span>已有账号？</span>
          <el-link type="primary" :underline="false" @click="goToLogin">立即登录</el-link>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { HomeFilled } from '@element-plus/icons-vue'
import { userAPI } from '../api'

const router = useRouter()
const loading = ref(false)
const registerFormRef = ref(null)

// 注册表单
const registerForm = reactive({
  username: '',
  email: '',
  password: '',
  confirmPassword: '',
  agreement: false
})

// 注册表单验证规则
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度应为3-20个字符', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        // 检查用户名是否已存在
        // 实际项目中应该调用API验证
        callback()
      },
      trigger: 'blur'
    }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入有效的邮箱地址', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度至少为6个字符', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        // 可以添加密码强度校验
        callback()
      },
      trigger: 'blur'
    }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== registerForm.password) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ],
  agreement: [
    { required: true, message: '请阅读并同意用户协议和隐私政策', trigger: 'change' }
  ]
}

// 处理注册
const handleRegister = async () => {
  try {
    // 表单验证
    await registerFormRef.value.validate()
    loading.value = true
    
    // 调用注册API
    // 注意：这里使用模拟数据，实际应该调用真实API
    // const response = await userAPI.register(registerForm)
    
    // 模拟注册成功
    setTimeout(() => {
      ElMessage.success('注册成功，请登录')
      
      // 跳转到登录页面
      router.push('/login')
      
    }, 1500)
    
  } catch (error) {
    console.error('注册失败:', error)
    ElMessage.error('注册失败，请稍后重试')
  } finally {
    loading.value = false
  }
}

// 跳转到登录页面
const goToLogin = () => {
  router.push('/login')
}
</script>

<style scoped>
.register-container {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: #FFFFFF;
  padding: 20px;
}

.register-wrapper {
  width: 100%;
  max-width: 400px;
  background: #FFFFFF;
  border-radius: 16px;
  padding: 40px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  border: 1px solid #e0e0e0;
}

.register-header {
  text-align: center;
  margin-bottom: 30px;
}

.logo-icon {
  font-size: 48px;
  color: #409eff;
  margin-bottom: 16px;
}

.system-title {
  font-size: 24px;
  font-weight: bold;
  color: #333333;
  margin-bottom: 8px;
}

.subtitle {
  font-size: 16px;
  color: #606266;
}

.register-form {
  margin-bottom: 20px;
}

:deep(.el-form-item) {
  margin-bottom: 20px;
}

:deep(.el-input__wrapper) {
  background-color: #FFFFFF;
  border-color: #dcdfe6;
  border-radius: 8px;
}

:deep(.el-input__wrapper:hover) {
  border-color: #c0c4cc;
  box-shadow: none;
}

:deep(.el-input__wrapper.is-focus) {
  border-color: #409eff;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
}

:deep(.el-input__inner) {
  color: #303133;
}

:deep(.el-input__placeholder) {
  color: #c0c4cc;
}

.agreement-checkbox :deep(.el-checkbox__label) {
  color: #606266;
}

:deep(.el-checkbox__input.is-checked .el-checkbox__inner) {
  background-color: #409eff;
  border-color: #409eff;
}

:deep(.el-checkbox__input.is-checked + .el-checkbox__label) {
  color: #409eff;
}

.register-button {
  width: 100%;
  height: 40px;
  font-size: 16px;
  border-radius: 8px;
  background-color: #409eff;
  border-color: #409eff;
}

.register-button:hover {
  background-color: #66b1ff;
  border-color: #66b1ff;
}

.register-button.is-loading {
  background-color: #409eff;
  border-color: #409eff;
}

.login-link {
  text-align: center;
  margin-top: 20px;
  color: #606266;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .register-wrapper {
    padding: 25px;
  }
  
  .system-title {
    font-size: 20px;
  }
  
  .subtitle {
    font-size: 14px;
  }
  
  .logo-icon {
    font-size: 36px;
  }
}
</style>